package com.app.newiptv.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import com.alibaba.fastjson.JSON;
import com.app.newiptv.R;
import com.app.newiptv.adaper.HomeChannelsAdapter;
import com.app.newiptv.bean.AdultBean;
import com.app.newiptv.bean.ChannelType;
import com.app.newiptv.bean.RequestDto;
import com.app.newiptv.callback.OnItemClickListener;
import com.app.newiptv.callback.SetFocusCallBack;
import com.app.newiptv.utils.Paths;
import com.app.newiptv.view.FocusGridLayoutManager;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
import okhttp3.Call;

import static com.app.newiptv.activity.Contast.TAG;

public class AdultActivity extends Activity implements SetFocusCallBack {
    private RecyclerView channels;
    private ImageView focusView;
    private ChannelType channelTypeInfo;
    private HomeChannelsAdapter channelsAdapter;
    private List<AdultBean> adultBeanList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_home);
        initView();
        Log.i(TAG, "onResponse: " + adultBeanList);
        channels.setLayoutManager(new FocusGridLayoutManager(this, 5));


    }


    private void initView() {
        channels = (RecyclerView) findViewById(R.id.activity_home_channle_category);
        focusView = (ImageView)findViewById(R.id.focus_view);

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (adultBeanList == null) {
            OkHttpUtils.post().addParams("uid", Contast.USE_UID).addParams("token", Contast.USE_TOKEN).url(Paths.HOSTPATH+Paths.CHANNEL_TYPES).build().execute(new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {

                }

                @Override
                public void onResponse(String response, int id) {
                    try {
                        JSONObject object = new JSONObject(response);
                        RequestDto dto = new RequestDto();
                        if (object.has("code")) {
                            int code = object.getInt("code");
                            dto.setCode(code);
                            if (1 == code) {
                                Log.i(TAG, "onResponse: " + response);
                                channelTypeInfo = JSON.parseObject(response,ChannelType.class);
                                List<ChannelType.DataBean> data = channelTypeInfo.getData();
                                List<AdultBean> beans = new ArrayList<>();
                                for (int i = 0;i< data.size(); i ++){
                                    String tId = data.get(i).getT_id();

                                    if (tId.equals("8") || tId.equals("9") || tId.equals("10") || tId.equals("11") || tId.equals("12")){
                                        Log.i(TAG, "picture: " + data.get(i).getCs().get(0).getPic());
                                        if (tId.equals("12")) {
                                            beans.add(0, new AdultBean(tId,
                                                    data.get(i).getT_name(),
                                                    data.get(i).getCs().get(0).getPic()));
                                        } else {
                                            beans.add(new AdultBean(tId,
                                                    data.get(i).getT_name(),
                                                    data.get(i).getCs().get(0).getPic()));
                                        }
                                    }
                                }
                                adultBeanList = beans;
                                channelsAdapter = new HomeChannelsAdapter(adultBeanList, AdultActivity.this, focusView);
                                channels.setAdapter(channelsAdapter);
                                channelsAdapter.setOnItemClickListener(new OnItemClickListener() {
                                    @Override
                                    public void onItemClick(View view) {
                                        Intent intent = new Intent(AdultActivity.this, VodActivity.class);
                                        intent.putExtra("type_big_id", channelsAdapter.currentChannle.getType());
                                        startActivity(intent);
                                    }

                                    @Override
                                    public void onItemLongClick(View view) {

                                    }
                                });
                            } else {
                                Log.i("otherLogin", "analysisData: other device login");
                                Intent intent = new Intent();
                                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                intent.setAction("com.scott.sayhi");
                                Contast.globalContext.sendBroadcast(intent);
//                                        mCallback.onErrer(code, "");
                            }
                        } else {
                        }


                    }catch (Exception e) {

                    }

                }
            });
        } else {
            channelsAdapter.setChannels(adultBeanList);
            channelsAdapter.notifyDataSetChanged();
        }
    }

    public class SetOnKey implements View.OnKeyListener {

        @Override
        public boolean onKey(View view, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                return true;
            } else if (keyCode == KeyEvent.KEYCODE_BACK) {
                return true;
            }
            return false;
        }
    }
    @Override
    public void setViewFocusAble() {
    }
}
